﻿<%Sub API_NO_ALLOW:End Sub%>
<!--#include file="api_.asp"-->
<%
'======================
'	七色虎程序API接口
'	版本:	1
'	时间:	2012-04-15
'======================

'1.2.6正式版开始开启API
'接口目的:实现扩展
'设计考量:前后台样式有区别,扩展内容与后台无关
'目前缺点:只做了数据库处理,不够完善
'改进方向:配置获取,样式控制,SQL过滤
'
'使用方法:
'引入API:
'<!--#include file="api.asp"-->
'开始:
'Set hu = new huAPI
'调用:
'hu.conn(sql)
'结束:
'Set hu = Nothing

'======================
'API类
'前台类: huAPI
'后台类: huAdmin
'API类属性
'ConnCount			数据库查询次数
'LastTime			最近一次查询耗时
'ConnTime			数据库查询总耗时
'ExecTime			系统执行时间
'Sid				当前sid名称(前台可自定义,后台也会考虑自定义)
'Debug				开发模式
'API类方法
'W(str)				输出内容
'R(url)				页面跳转
'Conn(sql)			执行SQL
'Log(str)			日志
'======================
'API变量
'API_NOW			当前详细时间,用于时间校正
'API_DATE			当前日期
'API_TIME			当前时间
'API_START			程序开始执行时间(Timer)
'======================

'前台API
Class huAPI
	Private ver_			'版本号
	Private sid_			'当前sid名称
	Private fso_			'文件操作对象
	Private conn_			'数据库对象
	Private count_			'数据库查询次数
	Private debug_			'开发模式
	Private logPath_		'日志目录
	Private lastExec_		'最近一次查询耗时
	Private connExec_		'总查询耗时
	'Private connTimeout_	'连接超时(用于记录超时,改进查询)
	'Private style_			'样式控制
	'Private config_		'配置控制

	Private Sub Class_Initialize
		ver_ = 1
		count_ = 0
		lastExec_ = 0
		connExec_ = 0
		sid_ = API_SID
		debug_ = False
		logPath_ = ""
		Set conn_=Server.CreateObject("ADODB.CONNECTION")
		conn_.open "Provider=Microsoft.Jet.OLEDB.4.0;Jet OLEDB:Database PASSWORD=;Data Source="&Server.MapPath(""&db&"")
	End Sub

	Private Sub Class_Terminate
		conn_.close
		Set conn_ = Nothing
		Set fso_ = Nothing
	End Sub

	'版本
	Public Property Get Ver
		Ver = ver_
	End Property

	'Sid名称
	Public Property Get Sid
		Sid = sid_
	End Property

	'数据库查询次数
	Public Property Get ConnCount
		ConnCount = count_
	End Property

	'系统执行时间
	Public Property Get ExecTime
		ExecTime = FormatNumber((Timer-API_START), 3, true)
	End Property

	'最近一次查询耗时
	Public Property Get LastTime
		LastTime = FormatNumber((lastExec_), 3, true)
	End Property

	'数据库查询总耗时
	Public Property Get ConnTime
		ConnTime = FormatNumber((connExec_), 3, true)
	End Property

	'获取开发模式
	Public Property Get Debug
		Debug = debug_
	End Property

	'设置开发模式
	Public Property Let Debug(bool)
		If bool = True Then
			debug_ = True
			Set fso_ = CreateObject("Scripting.FileSystemObject")
		Else
			debug_ = False
			Set fso_ = Nothing
		End If
	End Property

	'获取日志目录
	Public Property Get LogPath
		LogPath = logPath_
	End Property

	'设置开发模式
	Public Property Let LogPath(path)
		path = Server.MapPath(path)
		If TypeName(fso_) = "FileSystemObject" Then
			If fso_.FolderExists(path) Then logPath_ = path
		Else
			logPath_ = ""
		End If
	End Property

	'输出信息
	Public Sub W(str)
		Response.Write str
	End Sub

	'页面跳转
	Public Sub R(url)
		Response.Redirect url
	End Sub

	'执行SQL语句
	Public Function Conn(sql)
		If debug_ Then lastExec_ = Timer
		Conn = conn_.Execute(sql)
		If debug_ Then'开发状态下记录查询
			lastExec_ = Timer - lastExec_
			connExec_ = connExec_ + lastExec_
			count_ = count_ + 1
			Log("[SQL:"&FormatNumber((lastExec_), 3, true)&"]"&sql)
		End If
	End Function

	'写日志
	Public Function Log(str)
		Dim file,f
		If logPath_<>"" Then
			file = logPath_&"\"&getDate_()&".log"
			If Not fso_.FileExists(file) Then Call fso_.CreateTextFile(file,true)
			Set f = fso_.OpenTextFile(file, 8)
			f.Write "["&API_TIME&"] "&str&chr(13)&chr(10)
			f.Close
			Set f=Nothing
		End If
	End Function

End Class

'后台API
Class huAdmin
	Private ver_			'版本号
	Private sid_			'当前sid名称
	Private fso_			'文件操作对象
	Private conn_			'数据库对象
	Private count_			'数据库查询次数
	Private debug_			'开发模式
	Private logPath_		'日志目录
	Private lastExec_		'最近一次查询耗时
	Private connExec_		'总查询耗时

	Private Sub Class_Initialize
		ver_ = 1
		count_ = 0
		lastExec_ = 0
		connExec_ = 0
		sid_ = API_ADMIN_SID
		debug_ = False
		logPath_ = ""
		Set conn_=Server.CreateObject("ADODB.CONNECTION")
		conn_.open "Provider=Microsoft.Jet.OLEDB.4.0;Jet OLEDB:Database PASSWORD=;Data Source="&Server.MapPath(""&dbm&"")
	End Sub

	Private Sub Class_Terminate
		conn_.close
		Set conn_ = Nothing
		Set fso_ = Nothing
	End Sub

	'版本
	Public Property Get Ver
		Ver = ver_
	End Property

	'数据库查询次数
	Public Property Get ConnCount
		ConnCount = count_
	End Property

	'系统执行时间
	Public Property Get ExecTime
		ExecTime = FormatNumber((Timer-API_START), 3, true)
	End Property

	'最近一次查询耗时
	Public Property Get LastTime
		LastTime = FormatNumber((lastExec_), 3, true)
	End Property

	'数据库查询总耗时
	Public Property Get ConnTime
		ConnTime = FormatNumber((connExec_), 3, true)
	End Property

	'获取开发模式
	Public Property Get Debug
		Debug = debug_
	End Property

	'设置开发模式
	Public Property Let Debug(bool)
		If bool = True Then
			debug_ = True
			Set fso_ = CreateObject("Scripting.FileSystemObject")
		Else
			debug_ = False
			Set fso_ = Nothing
		End If
	End Property

	'获取日志目录
	Public Property Get LogPath
		LogPath = logPath_
	End Property

	'设置开发模式
	Public Property Let LogPath(path)
		path = Server.MapPath(path)
		If TypeName(fso_) = "FileSystemObject" Then
			If fso_.FolderExists(path) Then logPath_ = path
		Else
			logPath_ = ""
		End If
	End Property

	'输出信息
	Public Sub W(str)
		Response.Write str
	End Sub

	'页面跳转
	Public Sub R(url)
		Response.Redirect url
	End Sub

	'执行SQL语句
	Public Function Conn(sql)
		If debug_ Then lastExec_ = Timer
		Conn = conn_.Execute(sql)
		If debug_ Then'开发状态下记录查询
			lastExec_ = Timer - lastExec_
			connExec_ = connExec_ + lastExec_
			count_ = count_ + 1
			Log("[SQL:"&FormatNumber((lastExec_), 3, true)&"]"&sql)
		End If
	End Function

	'写日志
	Public Function Log(str)
		Dim file,f
		If logPath_<>"" Then
			file = logPath_&"\"&getDate_()&".log"
			If Not fso_.FileExists(file) Then Call fso_.CreateTextFile(file,true)
			Set f = fso_.OpenTextFile(file, 8)
			f.Write "["&API_TIME&"] "&str&chr(13)&chr(10)
			f.Close
			Set f=Nothing
		End If
	End Function

End Class

%>